﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using BCCommon;
using BCData.Common.Dictionary;
using BCDto.Common.Dictionary;
using BCEntity.Common.Dictionary;
using BCService.Common.Dictionary;
using BCService.Utils;

namespace BCService.WebDomainService.Dictionary
{
    public class DictionaryReferenceService : IDictionaryReferenceService
    {
        #region 私有变量

        private readonly IDictionaryReferenceData dictionaryReferenceData;

        #endregion 私有变量

        #region 构造函数

        public DictionaryReferenceService(IDictionaryReferenceData dictionaryReferenceData)
        {
            this.dictionaryReferenceData = dictionaryReferenceData;
        }
        
        #endregion 构造函数

        #region 公共方法

        #region 增删改

        public async Task<int> InsertAsync(DictionaryReferenceRequestDto dto)
        {
            var entity = dto.As<DictionaryReferenceEntity>();
            entity.SearchKey = !string.IsNullOrWhiteSpace(dto.ParentName)
                ? $"{dto.ParentName}_{dto.Name}{(!string.IsNullOrWhiteSpace(dto.TypeName) ? "_" + dto.TypeName : "")}"
                : $" {dto.Name}{(!string.IsNullOrWhiteSpace(dto.TypeName) ? "_" + dto.TypeName : "")}";

            var data = await dictionaryReferenceData.GetAsync(entity.SearchKey);
            if (data.Count() > 0)
            {
                throw new ArgumentException("索引已存在");
            }
            var res = await dictionaryReferenceData.InsertAsync(entity);
            if (res > 0)
                return res;

            throw new ArgumentException("新增字典失败");
        }

        public async Task<int> UpdateAsync(int id, DictionaryReferenceRequestDto dto)
        {
            var dictionaryInfo = await dictionaryReferenceData.GetByIdAsync(id);
            if (dictionaryInfo == null)
                throw new ArgumentException("字典不存在");

            var entity = dto.As<DictionaryReferenceEntity>();
            entity.SearchKey = !string.IsNullOrWhiteSpace(dto.ParentName)
                ? $"{dto.ParentName}_{dto.Name}{(!string.IsNullOrWhiteSpace(dto.TypeName) ? "_" + dto.TypeName : "")}"
                : $" {dto.Name}{(!string.IsNullOrWhiteSpace(dto.TypeName) ? "_" + dto.TypeName : "")}";
            entity.DictionaryReferenceId = id;

            var res = await dictionaryReferenceData.UpdateAsync(entity);
            if (res > 0)
                return res;

            throw new ArgumentException("修改字典失败");
        }

        public async Task<int> DeleteAsync(int id)
        {
            var res = await dictionaryReferenceData.DeleteAsync(id);
            if (res > 0)
                return res;

            throw new ArgumentException("删除字典失败");
        }

        #endregion 增删改

        #region 查询

        public async Task<List<DictionaryReferenceDto>> GetAll()
        {
            var datas = await dictionaryReferenceData.GetAsync();
            return datas.As<List<DictionaryReferenceDto>>();
        }

        public async Task<DictionaryReferenceDto> GetById(int dictionaryReferenceId)
        {
            var data = await dictionaryReferenceData.GetByIdAsync(dictionaryReferenceId);
            return data.As<DictionaryReferenceDto>();
        }

        public async Task<PageableList<DictionaryReferenceDto>> PageAll(int pageIndex, int pageSize, bool ascending)
        {
            var (dataCount, datas) = await dictionaryReferenceData.GetAsync(pageIndex, pageSize, ascending);

            return new PageableList<DictionaryReferenceDto>()
            {
                Count = dataCount,
                Items = datas.As<List<DictionaryReferenceDto>>()
            };
        }

        public async Task<List<DictionaryReferenceDto>> GetBySearchkeyAsync(string searchkey)
        {
            var data = await dictionaryReferenceData.GetAsync(searchkey, string.Empty);
            return data.As<List<DictionaryReferenceDto>>().ToList();
        }

        public async Task<List<DictionaryReferenceDto>> GetByTypeNameAsync(string typeName)
        {
            var data = await dictionaryReferenceData.GetByTypeNameAsync(typeName);
            return data.As<List<DictionaryReferenceDto>>().ToList();
        }

        public async Task<List<DictionaryReferenceDto>> GetByParentNameAsync(string parentName)
        {
            var data = await dictionaryReferenceData.GetByParentNameAsync(parentName);
            return data.As<List<DictionaryReferenceDto>>().ToList();
        }

        public async Task<PageableList<DictionaryReferenceDto>> GetPageableList(int pageIndex, int pageSize,
            bool ascending, string itemValue, string typeValue)
        {
            var (dataCount, dataList) = await dictionaryReferenceData.GetAsync(pageIndex, pageSize, ascending, itemValue, typeValue);

            return new PageableList<DictionaryReferenceDto>()
            {
                Count = dataCount,
                Items = dataList.As<List<DictionaryReferenceDto>>(),
            };
        }

        public List<DictionaryReferenceDto> GetByTypeNameAndParentNameAsync(string typeName, string parentName)
        {
            var data = dictionaryReferenceData.GetByTypeNameAndParentNameAsync(typeName, parentName);
            var result = data.GetAwaiter().GetResult();
            return result.As<List<DictionaryReferenceDto>>();
        }

        public async Task<DictionaryReferenceDto> GetAsync(string searchKey)
        {
            var data = await dictionaryReferenceData.GetEntityAsync(searchKey);
            return data.As<DictionaryReferenceDto>();
        }

        #endregion 查询

        #endregion 公共方法
    }
}